package com.edu.web.servlet;

import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import com.edu.service.impl.UserServiceImpl;

/**
 * Servlet implementation class indexServlet
 */
@WebServlet("/indexServlet")
public class indexServlet extends HttpServlet {
	
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		 request.setCharacterEncoding("UTF-8");
		//实例化对象
		UserServiceImpl userServletImpl=new UserServiceImpl();
		//获取登录信息，验证身份
		HttpSession session=request.getSession();
		userServletImpl=new UserServiceImpl();
		//返回全部用户
		boolean i =userServletImpl.queryNameAndPass(request.getParameter("username"), request.getParameter("userpass"));
		if(i==true) {
			session.setAttribute("Identity", "管理员");
			Cookie newCookie=new Cookie("Identity","管理员");
			response.addCookie(newCookie);
			newCookie.setMaxAge(108000);
			Cookie[] aCookie=request.getCookies();
			request.setAttribute("userall", userServletImpl.queryByAUser());
			request.getRequestDispatcher("index.jsp").forward(request, response);
			return;
		}
		request.getRequestDispatcher("login.jsp").forward(request, response);
	}

	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		doGet(request, response);
	}

}
